package router

import (
	"background/http/api/helper"
	"background/model/back/entity"
	"background/model/constant"
	"background/utilx/ginx"
	"github.com/gin-gonic/gin"
)

func (p CloudUser) InitInfoRouter(group *gin.RouterGroup) {
	gg := group.Group("/")
	// 关闭验证
	gg.GET("/info", p.info())
	gg.GET("/traffic", p.traffic())
	gg.GET("/order", p.order())
	gg.GET("/subscribe", p.subscribe())
	//==================
	gg.POST("/login", p.login())
	gg.POST("/register", p.register())
	gg.POST("/resetPassword", p.register())
}

// ======================
func (p CloudUser) info() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var temp entity.CloudUser
		if value, exists := ctx.Get(constant.UserInfo); exists {
			temp = value.(entity.CloudUser)
		} else {
			helper.ErrorWithMsg(ctx, 400, "failed:no user")
			return
		}
		data, err := p.api.Info(ctx.Request.Context(), temp.ID)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessWithData(ctx, data)
	}
}

// ======================
func (p CloudUser) traffic() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var temp entity.CloudUser
		if value, exists := ctx.Get(constant.UserInfo); exists {
			temp = value.(entity.CloudUser)
		} else {
			helper.ErrorWithMsg(ctx, 400, "failed:no user")
			return
		}
		data, err := p.api.Traffic(ctx.Request.Context(), temp.ID)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessWithData(ctx, data)
	}
}
func (p CloudUser) order() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var temp entity.CloudUser
		if value, exists := ctx.Get(constant.UserInfo); exists {
			temp = value.(entity.CloudUser)
		} else {
			helper.ErrorWithMsg(ctx, 400, "failed:no user")
			return
		}
		data, err := p.api.Order(ctx.Request.Context(), temp.ID)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessWithData(ctx, data)
	}
}
func (p CloudUser) subscribe() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var temp entity.CloudUser
		if value, exists := ctx.Get(constant.UserInfo); exists {
			temp = value.(entity.CloudUser)
		} else {
			helper.ErrorWithMsg(ctx, 400, "failed:no user")
			return
		}
		data, err := p.api.Subscribe(ctx.Request.Context(), temp.ID)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessWithData(ctx, data)
	}
}

// ===========
func (p CloudUser) login() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec entity.CloudUser
		if ginx.BodyJsonWithErr[entity.CloudUser](ctx, &rec) {
			return
		}
		var (
			data any
			err  error
		)
		//forceRedis := ctx.GetBool(constant.MediaRedisForce)
		//isAdmin := ctx.GetBool(constant.MediaIsAdmin)
		data, err = p.api.Login(ctx.Request.Context(), rec)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessData(ctx, data)
	}
}

// ===========
func (p CloudUser) register() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec entity.CloudUser
		if ginx.BodyJsonWithErr[entity.CloudUser](ctx, &rec) {
			return
		}
		var (
			data any
			err  error
		)
		err = p.api.Register(ctx.Request.Context(), rec)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessData(ctx, data)
	}
}
func (p CloudUser) resetPassword() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec entity.CloudUser
		if ginx.BodyJsonWithErr[entity.CloudUser](ctx, &rec) {
			return
		}
		var (
			data any
			err  error
		)
		err = p.api.ResetPassword(ctx.Request.Context(), rec)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessData(ctx, data)
	}
}
